import cv2
import tkinter as tk
import tarckbar
import addlogo
import grabc
import Haar
from tkinter import ttk
from tkinter import filedialog

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("图像处理工具箱")
        self.geometry("500x400")
        self.button1 = tk.Button(self, text="亮度与对比度调节", command=lambda: self.select_file(1))
        self.button1.grid()
        self.button2 = tk.Button(self, text="添加logo", command=lambda: self.select_file(2))
        self.button2.grid()
        self.button3 = tk.Button(self, text="前后景分离", command=lambda: self.select_file(3))
        self.button3.grid()
        self.button4 = tk.Button(self, text="人脸识别", command=lambda: self.select_file(4))
        self.button4.grid()

        self.button1.grid(row=0, column=0, pady=30)
        self.button2.grid(row=1, column=0, pady=30)
        self.button3.grid(row=2, column=0, pady=30)
        self.button4.grid(row=3, column=0, pady=30)
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)
        self.grid_rowconfigure(2, weight=1)
        self.grid_rowconfigure(3, weight=1)
        self.lbl_file = ttk.Label(self, text="")
        self.lbl_file.grid()

    def select_file(self, flag):
        filetypes = (("PNG files", "*.png"), ("JPEG files", "*.jpg"))
        filename = filedialog.askopenfilename(title="请选择图片", filetypes=filetypes)
        if filename:
            img = cv2.imread(filename)
            if img is not None:
                print(img.shape)
                if flag == 1:
                    print("执行1任务")
                    tarckbar.change(img)
                elif flag == 2:
                    print("执行2任务")
                    addlogo.add(img)
                elif flag == 3:
                    print("执行3任务")
                    grabc.separate(img) 
                else:
                    print("执行4任务")
                    Haar.identify(img)
            else:
                text = "打开错误 请检查图片是否存在或者是否有中文路径"
                self.lbl_file.configure(text=text)

if __name__ == "__main__":
    app = App()
    app.mainloop()